package com.fyzk.activity.lottery.jc.football.newJz.data;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.fyzk.base.BaseActivity;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.GlideUtils;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import com.wf.domain.UrlDef;
import http.StringResultBean;

/**
 * Created by fanxs11 on 16/4/26.
 * <p>
 * 比分直播详情
 */
public class ScoreDetailActivity extends BaseActivity implements ViewPager.OnPageChangeListener, CompoundButton.OnCheckedChangeListener {

    private TopTitleView topView;
//    private ScoreInfo info;

    private ImageView homeLogo_iv;
    private ImageView awayLogo_iv;
    private TextView score_tv;
    private TextView home_tv;
    private TextView date_tv;
    private TextView away_tv;

    private RadioButton rb1;
    protected RadioButton rb2;
    private RadioButton rb3;
    protected RadioButton rb4;
//    private RadioButton rb5;

    private ViewPager viewPager;
    private ScorePagerAdapter pagerAdapter;
    private ArrayList<View> views = new ArrayList<>();
    /**
     * 当前选项卡位置
     */
    private int curPage = 0;


    protected String matchNum = "20160511001";

    private AnalysisView analysisView;
    private RankingView rankingView;
    private OuPeiView europeView;
    private OuPeiView asiaView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.score_detail_layout);

//        info = (ScoreInfo) getIntent().getSerializableExtra("matchNum");
        matchNum = getIntent().getStringExtra("matchNum");

        initView();

        net();
    }

    private void initView() {

        topView = (TopTitleView) findViewById(R.id.score_detail_topView);
        topView.setCenterText(getTopTitle());
        topView.setRightImg(R.drawable.hm_refresh_click);
        topView.setOnTitleClickListener(this);

        homeLogo_iv = (ImageView) findViewById(R.id.score_detail_homelogo_iv);
        awayLogo_iv = (ImageView) findViewById(R.id.score_detail_awaylogo_iv);
        score_tv = (TextView) findViewById(R.id.score_detail_score_tv);
        home_tv = (TextView) findViewById(R.id.score_detail_home_tv);
        date_tv = (TextView) findViewById(R.id.score_detail_date_tv);
        away_tv = (TextView) findViewById(R.id.score_detail_away_tv);

        rb1 = (RadioButton) findViewById(R.id.score_detail_rb1);
        rb2 = (RadioButton) findViewById(R.id.score_detail_rb2);
        rb3 = (RadioButton) findViewById(R.id.score_detail_rb3);
        rb4 = (RadioButton) findViewById(R.id.score_detail_rb4);
//        rb5 = (RadioButton) findViewById(R.id.score_detail_rb5);

        rb1.setOnCheckedChangeListener(this);
        rb2.setOnCheckedChangeListener(this);
        rb3.setOnCheckedChangeListener(this);
        rb4.setOnCheckedChangeListener(this);
//        rb5.setOnCheckedChangeListener(this);

        analysisView = new AnalysisView(getContext());
        rankingView = new RankingView(getContext(), this);
        europeView = new OuPeiView(getContext(), 0);
        asiaView = new OuPeiView(getContext(), 1);
        views.add(analysisView);
        if (isShowRanking()) {
            views.add(rankingView);
        }
        views.add(europeView);
        if (isShowQuery()) {
            views.add(asiaView);
        }

        viewPager = (ViewPager) findViewById(R.id.score_detail_viewpager);
        pagerAdapter = new ScorePagerAdapter(views);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(this);


    }

    protected String getTopTitle() {
        return "竞彩分析";
    }

    // 是否显示积分数据
    protected boolean isShowRanking() {
        return true;
    }
    // 是否显示压盘数据
    protected boolean isShowQuery() {
        return true;
    }

    @Override
    public void onRightClick() {
        net();
    }

    // pageview
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int page) {
        curPage = page;
        setXuanXiangKa(page);

    }

    /**
     * 选项卡选择
     *
     * @param id
     */
    private void setXuanXiangKa(int id) {

        switch (id) {
            case 0:
                rb1.setChecked(true);
                break;
            case 1:
                rb2.setChecked(true);
                break;
            case 2:
                rb3.setChecked(true);
                break;
            case 3:
                rb4.setChecked(true);
                break;
//            case 4:
//                rb5.setChecked(true);
//                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            switch (compoundButton.getId()) {
                case R.id.score_detail_rb1:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.score_detail_rb2:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.score_detail_rb3:
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.score_detail_rb4:
                    viewPager.setCurrentItem(3);
                    break;
                case R.id.score_detail_rb5:
                    viewPager.setCurrentItem(4);

                    break;
                //足球积分榜(切换选项卡)
                case R.id.jc_ranking_rb_total:
                    queryFootballRanking(1);
                    break;
                case R.id.jc_ranking_rb_home:
                    queryFootballRanking(2);

                    break;
                case R.id.jc_ranking_rb_away:
                    queryFootballRanking(3);

                    break;

                default:
                    break;

            }
        }

    }

    private void net() {

        queryGameRequest();
        queryGameInfoRequest();
        queryFootballRanking(1);
        queryFootballEuropeData();
        queryFootballAsiaData();
    }

    public static JCZQ_AnalyzeResponse analyzeResponse;
    public static JCZQ_MatchInfoResponse matchInfoResponse;
    private JCZQ_RankingResponse rankingResponse;
    private List<JCZQ_EuropeDataRespose> europeDataResponses = new ArrayList<>();
    private List<JCZQ_EuropeDataRespose> asiaDataResponses = new ArrayList<>();

    /* 查询对阵信息请求 */
    private void queryGameRequest() {
        try {
            JSONObject request = new JSONObject();
            request.put("matchNum", matchNum);
            httpPostJson(CommandUtil.MATCHINFO, Commands.MATCHINFO, request, false, UrlDef.TY_URL_NEW_DATA2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /* 查询比赛信息请求 */
    protected void queryGameInfoRequest() {
        try {
            JSONObject request = new JSONObject();
            request.put("matchNum", matchNum);
            httpPostJson(CommandUtil.FOOTBALLANALYZE, Commands.FOOTBALLANALYZE, request, false, UrlDef.TY_URL_NEW_DATA2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /* 足球积分榜 */
    private void queryFootballRanking(int rankingType) {
        try {
            JSONObject request = new JSONObject();
            request.put("matchNum", matchNum);
            request.put("rankingType", rankingType);
            httpPostJson(CommandUtil.FOOTBALLRANKING, Commands.FOOTBALLRANKING, request, false, UrlDef.TY_URL_NEW_DATA2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 欧亚析 足球欧赔数据
     */
    protected void queryFootballEuropeData() {
        try {
            JSONObject request = new JSONObject();
            request.put("matchNum", matchNum);
            httpPostJson(CommandUtil.FOOTBALLEUROPEDATA, Commands.FOOTBALLEUROPEDATA, request, false, UrlDef.TY_URL_NEW_DATA2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 欧亚析 足彩压盘数据
     */
    protected void queryFootballAsiaData() {
        try {
            JSONObject request = new JSONObject();
            request.put("matchNum", matchNum);
            httpPostJson(CommandUtil.FOOTBALLASIADATA, Commands.FOOTBALLASIADATA, request, false, UrlDef.TY_URL_NEW_DATA2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.MATCHINFO:
                matchInfoResponse = JSON.parseObject(resultBean.getData(), JCZQ_MatchInfoResponse.class);
                Log.i("response", "response: " + matchInfoResponse.getHomeLogo());

                Glide.with(getContext()).load(matchInfoResponse.getHomeLogo())
                        .error(R.drawable.jczq_analysis_wait_logo)
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .transform(new GlideUtils(getContext()))
                        .into(homeLogo_iv);
                Glide.with(getContext()).load(matchInfoResponse.getAwayLogo())
                        .error(R.drawable.jczq_analysis_wait_logo)
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .transform(new GlideUtils(getContext()))
                        .into(awayLogo_iv);
//                score_tv.setText(matchInfoResponse.getMatchDate());
                home_tv.setText(matchInfoResponse.getHomeTeam());
                date_tv.setText(matchInfoResponse.getMatchDate());
                away_tv.setText(matchInfoResponse.getAwayTeam());
                break;
            case CommandUtil.FOOTBALLANALYZE:
//                Log.i("response", "response0: " + result);
                analyzeResponse = JSON.parseObject(resultBean.getData(), JCZQ_AnalyzeResponse.class);

                analysisView.changeData();
                break;
            case CommandUtil.FOOTBALLRANKING:
                rankingResponse = JSON.parseObject(result, JCZQ_RankingResponse.class);

                rankingView.setInfos(rankingResponse.getData());
                break;
            case CommandUtil.FOOTBALLEUROPEDATA:
                JSONObject object = null;
                try {
                    object = new JSONObject(result.toString());
                    result = object.getString("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.i("response", "response1: " + result);
                europeDataResponses = JSON.parseArray(result, JCZQ_EuropeDataRespose.class);
                europeView.setInfos(europeDataResponses);
                break;
            case CommandUtil.FOOTBALLASIADATA:
                JSONObject object2 = null;
                try {
                    object2 = new JSONObject(result.toString());
                    result = object2.getString("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.i("response", "response2: " + result);
                asiaDataResponses = JSON.parseArray(result, JCZQ_EuropeDataRespose.class);
                asiaView.setInfos(asiaDataResponses);
                break;
            default:
                break;
        }
    }

}
